import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'

import CommentAdd from '../../components/comment-add/comment-add'
import CommentList from '../../components/comment-list/comment-list'
import { addComment, deleteComment, getComment } from '../../redux/actions'


export class App extends Component {

    static propTypes = {
        comments: PropTypes.array.isRequired,
        addComment: PropTypes.func.isRequired,
        deleteComment: PropTypes.func.isRequired,
        getComment: PropTypes.func.isRequired,
    }

    componentDidMount() {
        this.props.getComment()
    }



    render() {
        const { comments, addComment, deleteComment } = this.props
        return (
            <div>
                <header className="site-header jumbotron">
                    <div className="container">
                        <div className="row">
                            <div className="col-xs-12">
                                <h1>请发表对React的评论</h1>
                            </div>
                        </div>
                    </div>
                </header>
                <div className="container">
                    <CommentAdd addComment={addComment} />
                    <CommentList comments={comments} deleteComment={deleteComment} />
                </div>
            </div>
        )
    }
}


//接收 redux状态和行为
export default connect(
    state => ({ comments: state.comments }),
    { addComment, deleteComment, getComment }
)(App)